//Copyright (C) 2010 Workplicity, contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
package org.workplicity.marist.print;

import java.awt.Graphics2D;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import org.workplicity.marist.grounds.util.SeasonalPresets;
import org.workplicity.util.WorkDate;

/*
 * Large changes (remove this prior to upload to repo)
 * Alter constructor statements to determine if duplicates should be created
 * Altered DrawString function to only print to yOffset if carbonCopy is true.
 * Shoudl consider setting yOffest to zero and checking for that?
 *      Would remove a constructor but could potentially allow for more ineffecient code.
 */

public class PrintHelper {

    private Graphics2D g2d;
    private int yOffset;
    private final static int LINE_HEIGHT = 10;
    private FieldPosition pos = new FieldPosition(0);
    private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy h:mm a");
    private boolean carbonCopy;

    //Constructor used if only printing to one coordinate
    public PrintHelper(Graphics2D g2d){
        this.g2d = g2d;
        carbonCopy = false;
    }

    //Contructor to be used if printing with a "carbon copy"
    public PrintHelper(Graphics2D g2d, int yOffset) {
        this.g2d = g2d;
        this.yOffset = yOffset;
        carbonCopy = true;
    }

    public void DrawString(String line, int xPos, int yPos) {
        g2d.drawString(line, xPos, yPos);
        if (carbonCopy){
            g2d.drawString(line, xPos, yPos + yOffset);
        }
    }

    //Draws string at specified coordinates with the string truncated at the maxchars
    //If maxchars is 0 then it will not truncate
    public void PrintLine(String line, int xPos, int yPos, int maxChars) {
        if (line == null) {
            this.DrawString("NULL STRING, Please report error.", xPos, yPos);
        } else if (line.length() > maxChars && maxChars != 0) {
            this.DrawString(line.substring(0, maxChars), xPos, yPos);
        } else {
            this.DrawString(line, xPos, yPos);
        }
    }

    //Prints a string wrapping on spaces...
    //Possible bug, if a word is longer than maxChars, no more text wil be printed after that word.
    //TODO: add respect for newline characters
    public void PrintBox(String input, int xPos, int yPos, int maxLines, int maxChars) {
        String[] lines = fragmentLine(input);

        int lineCount = 0;

        for (int i = 0; i < maxLines && i < lines.length; i++) {
//            // If the line is too long, split in two pieces and
//            // draw them separately
//            if (lines[i].length() >= maxChars) {
//                // Break up the line on a space boundary
//                String first = lines[i].substring(0, maxChars);
//
//                int index = maxChars - 1;
//                while (lines[i].charAt(index) != ' ') {
//                    index--;
//                }
//
//                first = lines[i].substring(0, index);
//
//                this.DrawString(first, xPos, yPos + lineCount * LINE_HEIGHT);
//
//                lineCount++;
//
//                String second = lines[i].substring(index + 1).trim();
//                this.DrawString(second, xPos, yPos + lineCount * LINE_HEIGHT);
//            } else {
//                this.DrawString(lines[i], xPos, yPos + lineCount * LINE_HEIGHT);
//            }
//
            this.DrawString(lines[i], xPos, yPos + lineCount * LINE_HEIGHT);
            lineCount++;
        }
    }

    /**
     * Fragments the line into multiple lines on line breaks, removes the preset tags
     * and numbers the presets, if there are any.
     * @param line
     * @return
     */
    private String[] fragmentLine(String line) {
        String[] lines = line.split("\n");

//        int chore = 0;

        for (int i = 0; i < lines.length; i++) {
            int sindex = lines[i].indexOf(SeasonalPresets.SUFFIX_SPRING);
            if (sindex != -1) {
                lines[i] = lines[i].substring(0, sindex);
            }

            int windex = lines[i].indexOf(SeasonalPresets.SUFFIX_WINTER);
            if (windex != -1) {
                lines[i] = lines[i].substring(0, windex);
            }

//            chore++;
//            lines[i] = chore + "> " + lines[i];
        }

        return lines;
    }

    public void PrintDate(WorkDate workDate, int xPos, int yPos, boolean showTime) {
        if (workDate == null) {
            return;
        }

        StringBuffer buffer = formatter.format(workDate, new StringBuffer(), pos);

        this.PrintLine(buffer.toString(), xPos, yPos, 0);
    }
}
